package com.ztom.daily.n2022.m6;

import java.util.HashSet;
import java.util.Set;

/**
 * 独特的电子邮件地址
 * <p>
 * https://leetcode.cn/problems/unique-email-addresses/
 *
 * @author ZhangTao
 */
public class Day04NumUniqueEmails {

    public static void main(String[] args) {
        String[] s = {"test.email+alex@leetcode.com","test.email.leet+alex@code.com"};
        System.out.println(new Day04NumUniqueEmails().numUniqueEmails(s));
    }

    public int numUniqueEmails1(String[] emails) {
        Set<String> set = new HashSet<>();
        for (String email : emails) {
            String[] s = email.split("@");
            set.add(s[0].split("\\+")[0].replace(".", "") + "@" + s[1]);
        }
        return set.size();
    }

    public int numUniqueEmails(String[] emails) {
        Set<String> set = new HashSet<>();
        for (String email : emails) {
            int i = email.indexOf("@");
            String pre = email.substring(0, i).split("\\+")[0].replace(".", "");
            set.add(pre + email.substring(i));
        }
        return set.size();
    }
}
